<?php

namespace app\controller\api;

use app\BaseController;
use think\facade\Filesystem;

class Upload extends BaseController
{
    /**
     * 图片上传
     */
    public function image()
    {
        $file = $this->request->file('file');
        $data = $this->getData(['type' => 'img'], ['type' => 'require|in:img']);
        if ($file) {
            //验证文件
            $this->validate($this->request->file(), ['file' => 'fileSize:' . (1048576 * 10) . '|fileMime:image/jpeg,image/webp,image/png,image/gif|file']);
            // 上传到阿里云oss
            $savename = Filesystem::disk('aliyun')->putFile('xl_' . $data['type'], $file, function () {
                return date('Ym') . '/' . md5(uniqid(mt_rand(0, 99), true));
            });
            $result = [
                'type' => $file->getMime(),
                'extension' => $file->extension(),
                'url' => $savename,
                'full_url' => config('filesystem.disks.aliyun.url') . '/' . $savename,
            ];
            \Api::success($result);
        } else {
            \Api::error('没有上传文件');
        }
    }
}